/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javaseapp - Thread7
 * <p>Powered by webrx On 2021-08-26 14:53:15
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Thread7 {
    volatile static boolean flag = false;

    public static void work() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s启动了%n", tn);
        while (!flag) {
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.printf("....%n");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.printf("%s结束了...%n", tn);
    }

    public static void main(String[] args) {
        var t = new Thread(Thread7::work, "T1");
        t.start();

        try {
            TimeUnit.SECONDS.sleep(10);
            flag = true;//中断线程代码，结束线程
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
